package com.dartfar.base.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.dartfar.base.model.Role;
import com.dartfar.base.service.RoleService;
import com.dartfar.base.vo.DataGridVo;
import com.dartfar.base.vo.FunctionVo;
import com.dartfar.base.vo.RoleVo;
import com.dartfar.dictionary.vo.DictcatalogVo;
import com.dartfar.workflow.action.WorkflowProcessFunMountAction;

/**
 * 角色Action对象
 * @author 王琦
 */
@Component("roleAction")
@Scope("prototype")
public class RoleAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	static Logger logger = Logger.getLogger(RoleAction.class);

	
	/**
	 * Service对象
	 */
	@Resource
	private RoleService roleService;

	
	/**
	 * 页面传参
	 */
	
	private List<FunctionVo> functions;
	
	private RoleVo roleVo;
	private int id;
	private String name;
	private String code;
	private String des;
	private String result;
	
	private String inserted;
	private String updated;

	/**
	 * 页面跳转
	 */
	public String show(){
		//获取权限列表
		functions = getAutFunctions();
		return "show";
	}
	
	
	/**
	 * 保存 综合操作
	 * @param inserted, updated, deleted
	 * @return null
	 */
	public String save(){
		
		List<RoleVo> list;
		//新建键值
		if(inserted != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(inserted, RoleVo.class);
			for(Iterator<RoleVo> i =  list.iterator();i.hasNext();){
				RoleVo role = i.next();
				name = role.getName();
				code = role.getCode();
				des = role.getDes();
				add();
			}
		}
		//更新键值
		if(updated != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(updated, RoleVo.class);
			for(Iterator<RoleVo> i =  list.iterator();i.hasNext();){
				RoleVo role = i.next();
				id = role.getId();
				name = role.getName();
				code = role.getCode();
				des = role.getDes();
				edit();
			}
		}
		logger.info("执行完毕");
		return SUCCESS;
	}
	
	
	/**
	 * 添加角色
	 */
	public String add(){		
		Role role = new Role();
		role.setRoleName(name);
		role.setRoleCode(code);
		role.setRoleDes(des);
		roleService.save(role);
		
		return SUCCESS;
	}
	
	/**
	 * 修改角色
	 */
	public String edit(){
		Role role = roleService.get(id);
		role.setRoleName(name);
		role.setRoleCode(code);
		role.setRoleDes(des);
		roleService.update(role);
		return SUCCESS;
	}
	
	public String existUsersByRole(){
		if(roleService.existUsersByRoleId(id)){
			result="true";
		}else{
			result="false";
		}
		return SUCCESS;
	}
	
	/**
	 * 删除角色
	 */
	public String delete(){
		roleService.delete(roleService.get(id));
		return SUCCESS;
	}
	
	/**
	 * 读取角色列表
	 * @return RoleVo的Json字符串
	 */
	public String loadRoleData(){
		List<Role> roles = roleService.getListForPage(pageNo, pageSize);
		Long count = roleService.getCount();
		DataGridVo<RoleVo> dataGridVos = new DataGridVo<RoleVo>();
		List<RoleVo> roleVos = new ArrayList<RoleVo>();
		for(Iterator<Role> i = roles.iterator();i.hasNext();){
			Role role = i.next();
			RoleVo roleVo = new RoleVo();
			roleVo.setId(role.getRoleId());
			roleVo.setName(role.getRoleName());
			roleVo.setCode(role.getRoleCode());
			roleVo.setDes(role.getRoleDes());
			roleVos.add(roleVo);
		}
		dataGridVos.setTotal(count);
		dataGridVos.setRows(roleVos);
		String jsonResult = JSONObject.fromObject(dataGridVos).toString();
		logger.info("jsonResult: " + jsonResult);
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	
	/**
	 * 读取角色列表
	 * @return RoleVo的Json字符串
	 */
	public String loadRoleDataList(){
		List<Role> roles = roleService.getList();
		List<RoleVo> roleVos = new ArrayList<RoleVo>();
		for(Iterator<Role> i = roles.iterator();i.hasNext();){
			Role role = i.next();
			RoleVo roleVo = new RoleVo();
			roleVo.setId(role.getRoleId());
			roleVo.setName(role.getRoleName());
			roleVo.setCode(role.getRoleCode());
			roleVo.setDes(role.getRoleDes());
			roleVos.add(roleVo);
		}
		String jsonResult = JSONArray.fromObject(roleVos).toString();
		logger.info("jsonResult: " + jsonResult);
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	

	/**
	 * Gets And Sets
	 */	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}		
	
	public RoleVo getRoleVo() {
		return roleVo;
	}
	
	public void setRoleVo(RoleVo roleVo) {
		this.roleVo = roleVo;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<FunctionVo> getFunctions() {
		return functions;
	}

	public void setFunctions(List<FunctionVo> functions) {
		this.functions = functions;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}


	public String getCode() {
		return code;
	}


	public void setCode(String code) {
		this.code = code;
	}


	public String getDes() {
		return des;
	}


	public void setDes(String des) {
		this.des = des;
	}


	public String getInserted() {
		return inserted;
	}


	public void setInserted(String inserted) {
		this.inserted = inserted;
	}


	public String getUpdated() {
		return updated;
	}


	public void setUpdated(String updated) {
		this.updated = updated;
	}
	
}
